package com.tjw.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjw.dao.CheckGroupDao;
import com.tjw.entity.PageResult;
import com.tjw.entity.QueryPageBean;
import com.tjw.pojo.CheckGroup;
import com.tjw.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;

/**
 * @author tjw
 * @Date 2020/10/8
 */
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        List<CheckGroup> checkGroupList = checkGroupDao.pageQuery(queryString);
        PageInfo<CheckGroup> checkGroupPageInfo = new PageInfo<>(checkGroupList);
        return new PageResult(checkGroupPageInfo.getTotal(),checkGroupPageInfo.getList());
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer id = checkGroup.getId();
        if(checkitemIds !=null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkGroupId",id);
                map.put("checkItemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        checkGroupDao.deleteCheckGroupAndCheckItem(id);
        checkGroupDao.delete(id);
    }

    @Override
    public Integer[] findById(Integer id) {

        return checkGroupDao.findCheckGroupAndCheckItemByID(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
        Integer id = checkGroup.getId();
        checkGroupDao.deleteCheckGroupAndCheckItem(id);
        if(checkitemIds !=null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkGroupId",id);
                map.put("checkItemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
